<?php

/**
 * 后台首页小图模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: comment.class.php 1402 2011-03-03 10:15:34Z zhangwentao $
 * http://www.t-mac.org；
 */
class comment extends Model
{

    private $comment_table;
    private $article_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->comment_table = '#@__comment';
        $this->article_table = '#@__article';
    }

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addcomment($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->comment_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个首页小图信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getcommentInfo($id)
    {
        $rs = $this->db->getRow("SELECT a.*, b.title FROM $this->comment_table a LEFT JOIN $this->article_table b ON a.article_id = b.article_id WHERE a.comment_id = $id LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取所有首页小图
     * return article_class,pages
     */
    public function getCommentList($url = null, $comment_approved, $search_keyword)
    {
        if ($url == '')
            $url = 'index.php?m=admin/comment';
        $sql_count = "SELECT COUNT(*) FROM $this->comment_table WHERE 1=1 ";
        $sql = "SELECT comment_id, comment_author, comment_author_email, comment_author_ip, comment_content, comment_approved, comment_time FROM $this->comment_table WHERE 1=1 ";
        if ($comment_approved != 'none') {
            $sql_count .= "AND comment_approved='$comment_approved'";
            $sql .="AND comment_approved='$comment_approved'";
            $url .= "&comment_approved=$comment_approved";
        }

        if ($search_keyword != '') {
            $sql_count .= " AND comment_content LIKE '%" . $search_keyword . "%'";
            $sql .=" AND comment_content LIKE '%" . $search_keyword . "%'";
            $url .= "&search_keyword=$search_keyword";
        }       
        $url .= '&page=';
        
        $count = $this->db->getOne($sql_count);        
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(2);
        $limit = $pages->getSqlLimit();
        $sql .= " ORDER BY comment_id DESC LIMIT $limit";
        $rs = $this->db->getAll($sql);

        $type_array = array(
            '0' => '隐藏',
            '1' => '显示',
            'spam' => '垃圾评论'
        );
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['time'] = date('y/m/d H:i:s', $v['comment_time']);
                $rs[$k]['type'] = $type_array[$v['comment_approved']];
            }
        }

        //把文章的当前page写到cookies里
        HttpResponse::setCookie('comment_page', $pages->getNowPage());
        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无评论!";
        }

        $this->assign('rs', $rs);
        $this->assign('pageCurrent', $pages->getNowPage());
        $this->assign('page', $pages->show());
        $this->assign('ErrorMsg', $ErrorMsg);
        return $rs;
    }

    /**
     * del
     * @param int $id
     */
    public function delcomment($id)
    {
        $rs = $this->db->execute("DELETE FROM $this->comment_table WHERE comment_id in ($id)");
        return $rs;
    }

}